package org.itboys.ace.web.lasted;



import org.itboys.admin.dto.lasted.SundayUploadDto;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年2月9日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_文件上传_service。
 * 
 */
@Controller
@RequestMapping("/sunday/web/upload")
public class SundayUploadController extends BaseController {


	@Autowired
	private SundayUploadService uploadService;


	/**
	 * 上传文件主页（即列表页）
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/index" )
	public  String index(HttpServletRequest request,
						   @RequestParam(value="outClassId",required=true)Long outClassId,
						   @RequestParam(value="outClassName",required=false)String outClassName,
						   Model model){

		model.addAttribute("outClassId", outClassId);
		model.addAttribute("outClassName", outClassName);

		return "/ace/upload/index";
	}
	/**
	 * 上传文件主页（即列表页）。查看
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/index/view" )
	public  String index_view(HttpServletRequest request,
						   @RequestParam(value="outClassId",required=true)Long outClassId,
						   @RequestParam(value="outClassName",required=false)String outClassName,
						   Model model){

		model.addAttribute("outClassId", outClassId);
		model.addAttribute("outClassName", outClassName);

		return "/ace/upload/index_view";
	}

	/**
	* 单个文件上传
	 * @param request
	 * @param response
	 * @param id
	 * **/
	@RequestMapping("/delete")
	public void delete(HttpServletRequest request,
						HttpServletResponse response,
						@RequestParam(value="id",required=true)Long id ) {
		try {

			uploadService.delete(id);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(0, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}
	/**
	 * 单个文件上传
	 * @param request
	 * @param response
	 * @param file
	 */
	@RequestMapping("/uploadSingle")
	public void uploadSingle(HttpServletRequest request,
								 HttpServletResponse response,
								 @RequestParam(value="file",required=true)MultipartFile file ) {
		try {

			Map<String,String> result = uploadService.uploadSingle(file);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}

	/**
	 *单个文件上传。同时保持上传记录。后台应用
	 * @param request
	 * @param response
	 * @param file
	 * @param outClassId
	 * @param outClassName
	 * @param remark
	 */
	@RequestMapping("/uploadSingleWithRecord")
	public void uploadSingleWithRecord(HttpServletRequest request,
								 HttpServletResponse response,
								 @RequestParam(value="file",required=false)MultipartFile file,
								 @RequestParam(value="outClassId",required=true)Long outClassId,
								 @RequestParam(value="outClassName",required=false)String outClassName,
								@RequestParam(value="remark",required=false)String remark
								 ) {
		try {

			Map<String,String> result = uploadService.uploadSingleWithRecord(file,outClassId,outClassName,remark);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}

	/**
	 * 多个文件上传
	 * @param request
	 * @param response
	 * @param files
	 */
	@RequestMapping("/uploadMulti")
	public void uploadMulti(HttpServletRequest request,
								 HttpServletResponse response,
								 @RequestParam(value="file",required=true)MultipartFile[] files ) {
		try {

			List<Map<String,String>> result = uploadService.uploadMulti(files);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}
	/**
	 *多个文件上传。同时保持上传记录。后台应用
	 * @param request
	 * @param response
	 * @param files
	 * @param outClassId
	 * @param outClassName
	 * @param remark
	 */
	@RequestMapping("/uploadMultiWithRecord")
	public void uploadMultiWithRecord(HttpServletRequest request,
										   HttpServletResponse response,
										   @RequestParam(value="files",required=true)MultipartFile[] files,
										   @RequestParam(value="outClassId",required=true)Long outClassId,
										   @RequestParam(value="outClassName",required=true)String outClassName,
										   @RequestParam(value="remark",required=false)String remark
	) {
		try {

			List<Map<String,String>> result = uploadService.uploadMultiWithRecord(files,outClassId,outClassName,remark);
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgResultDO(result, CommonConstants.SUCCESS));
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderJsonporJson(response, null,
					ResultPageDOHelper.getMsgCodeResultDO(-1, CommonConstants.FAIL));
		}
	}

	/**
	 * 分页获取上传的文件
	 * @param request
	 * @param response
	 */
	@RequestMapping("/select")
	public void select(HttpServletRequest request,
							   HttpServletResponse response,
							   @RequestParam(value="outClassId",required=true)Long outClassId,
							   @RequestParam(value="outClassName",required=false)String outClassName

	) {
		try {

			PageResult<SundayUploadDto> result = uploadService.select(request,outClassId,outClassName);
			JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response,e.getMessage());
		}

	}

	/**
	 * 不分页获取上传文件
	 * @param request
	 * @param response
	 * @param outClassId
	 * @param outClassName
	 */
	@RequestMapping("/selectNoPage")
	public void selectNoPage(HttpServletRequest request,
							   HttpServletResponse response,
							   @RequestParam(value="outClassId",required=true)Long outClassId,
							   @RequestParam(value="outClassName",required=false)String outClassName
	) {
		try {

			List<SundayUploadDto> result = uploadService.selectNoPage(outClassId,outClassName);
			AjaxUtils.renderJson(response,result);
		} catch (Exception e) {
			e.printStackTrace();
			AjaxUtils.renderText(response,e.getMessage());
		}

	}
	/***------------配合kindeditor文本编辑器的上传组件使用**------------------------------------**/

	/**
	 *多个文件上传。同时保持上传记录。后台应用
	 * @param request
	 * @param response
	 */
	@RequestMapping("/kindeditor/upload/single")
	public void kindeditor_upload_single(HttpServletRequest request,
										  HttpServletResponse response,
										  @RequestParam(value="imgFile",required=true)MultipartFile imgFile
	) {
		try {
			Map<String,String> result = uploadService.uploadSingle(imgFile);
			//成功时{"error" : 0,"url" : "http://www.example.com/path/to/file.ext"}
			String url =result.get(SundayUploadService.UPLOAD_FULL_PATH);
			String message= "{\"error\":0,\"url\":\""+url+"\"}";
			AjaxUtils.renderJson(response,message);

		} catch (Exception e) {
			//失败时{"error" : 0,"url" : "错误信息"}
			String message= "{\"error\":0,\"url\":\""+CommonConstants.ERROMSG+"\"}";
			AjaxUtils.renderJson(response,message);
		}

		}


}
